package com.bnz.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 第一个拦截器
 */

public class FInterceptor implements HandlerInterceptor {

    // 1、调用处理器方法之前会先调用
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器：preHandle");
        return true; // 这里为true，下面两个才能够执行
    }

    // 2、执行完处理器方法后调用（现在，model并未渲染到界面->界面没出来）
    // 这一步完成后，才会有界面显示
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 判断现在访问的方法是否有视图
        if (null != modelAndView) {
            String viewName = modelAndView.getViewName();
            System.out.println("视图：" + viewName);
        }
        System.out.println("拦截器：postHandle");
    }

    // 3、页面渲染完成后，做统一收尾
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("拦截器：afterCompletion");
    }
}
